package com.xueyi.aixihu.service.impl;

import com.xueyi.aixihu.entity.vo.DtFeedBackTotalVo;
import com.xueyi.aixihu.entity.vo.DtFeedBackVo;
import com.xueyi.aixihu.entity.vo.DtUsersVo;
import com.xueyi.aixihu.entity.vo.DtyybmVo;
import com.xueyi.aixihu.mapper.LocalDtFeedBackMapper;
import com.xueyi.aixihu.reqeust.DtFindByFeedBackListReq;
import com.xueyi.aixihu.reqeust.InsertMessageContentReq;
import com.xueyi.aixihu.response.DtFeedBackResponse;
import com.xueyi.aixihu.response.base.BaseResponse;
import com.xueyi.aixihu.service.LocalDtFeedBackService;
import com.xueyi.aixihu.status.SystemCode;
import com.xueyi.aixihu.utils.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author yuyu
 * @date : 2022/8/16
 * @Description : 留言反馈的业务实现层
 */
@Service
public class LocalDtFeedBackServiceImpl implements LocalDtFeedBackService {

    @Autowired
    private LocalDtFeedBackMapper localDtFeedBackMapper;

    /**
     * 查询留言反馈的集合
     *
     * @param dtFindByFeedBackListReq
     * @return
     */
    @Override
    public DtFeedBackResponse findByFeedBackList(DtFindByFeedBackListReq dtFindByFeedBackListReq) {
        //创建响应类对象
        DtFeedBackResponse dtFeedBackResponse = new DtFeedBackResponse();
        //获取当前状态的总条数
        Integer count = localDtFeedBackMapper.findByFeedBackListCount(dtFindByFeedBackListReq, null);
        //如果总条数大于0再进行查询集合的mapper
        if (count > 0) {
            //初始化参数
            dtFindByFeedBackListReq.countStartIndex();
            //查询集合
            List<DtFeedBackVo> byFeedBackList = localDtFeedBackMapper.findByFeedBackList(dtFindByFeedBackListReq, null);
            //封装集合数据
            dtFeedBackResponse.setList(byFeedBackList);
            //封装条数
            dtFeedBackResponse.setTotal(count);
            //封装code
            dtFeedBackResponse.setCode(SystemCode.SUCCESS);
            //封装响应信息
            dtFeedBackResponse.setMsg("查询成功");
        } else {

            dtFeedBackResponse.setCode(SystemCode.SUCCESS);

            dtFeedBackResponse.setMsg("查询为空");

        }
        return dtFeedBackResponse;
    }

    /**
     * 查询留言反馈单条数据
     *
     * @param id
     * @return
     */
    @Override
    public BaseResponse<DtyybmVo> viewInformation(Integer id) {
        //如果id大于0时查询数据库
        if (id > 0) {
            DtyybmVo dtyybmVo = localDtFeedBackMapper.viewInformation(id);
            return BaseResponse.success(dtyybmVo);
        } else {
            //小于或等于0则报错
            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "查询失败", null);
        }
    }

    /**
     * 修改留言状态
     *
     * @param id
     * @param isValid
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public BaseResponse updateIsValid(Integer id, Integer isValid) {
        String str = "";
        //判断id是否为0
        if (id > 0) {
            //判断isValid是否为空为空就是删除功能就将isValid赋值为2
            if (isValid == null) {
                isValid = 2;
                str = "改为关闭状态";
            } else {
                //不为空就是审核状态将isValid修改为1
                isValid = isValid == 1 ? 0 : 1;
                if (isValid == 1) {
                    str = "改为正常状态";
                } else {
                    str = "改为未审核状态";
                }
            }
            localDtFeedBackMapper.updateIsValid(id, isValid);

            DtyybmVo dtyybmVo = localDtFeedBackMapper.viewInformation(id);
            //添加日志
            LogUtil.buildUserLog(id + "", "【留言管理】用户" + DtUsersVo.findCurrentUser().getUsername()
                    + "修改了id为" + dtyybmVo.getId() + "留言的状态" +
                    str, DtUsersVo.findCurrentUser(), dtyybmVo.getFromToAreaId());
            return BaseResponse.build(SystemCode.SUCCESS, "修改成功", null);
        } else {

            return BaseResponse.build(SystemCode.QUERY_CHECK_ERROR, "id不能为空", null);
        }
    }

    /**
     * 查询留言反馈的各种状态的条数
     *
     * @return
     */
    @Override
    public BaseResponse<DtFeedBackTotalVo> findFeedBackTotal(String name) {
        //获取用户管理地址的信息
        name = name.trim() == "" ? null : name;
        DtFeedBackTotalVo feedBackTotal = localDtFeedBackMapper.findFeedBackTotal(name, null);
        return BaseResponse.success(feedBackTotal);
    }

    /**
     * 回复留言信息
     *
     * @param id
     * @param content
     * @return
     */
    @Override
    public BaseResponse updateLeaving(Integer id, String content) {
        if (content != null) {
            localDtFeedBackMapper.updateLeaving(id, content);
            return BaseResponse.success(null);
        } else {
            return BaseResponse.build(100, "回复的内容不能为空", null);
        }
    }

    /**
     * 发送留言信息
     *
     * @param insertMessageContentReq
     */
    @Override
    public BaseResponse<Boolean> insertMessageContent(InsertMessageContentReq insertMessageContentReq) {
        localDtFeedBackMapper.insertMessageContent(insertMessageContentReq);
        return BaseResponse.build(SystemCode.SUCCESS, "发送成功", Boolean.TRUE);
    }
}
